﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Gwn.Library.MvpVm.Interfaces;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Logging;
using Gwn.Library.MvpVm.Extensions;

namespace Gwn.Library.MvpVm.Base
{
    /// <summary>
    /// Command base
    /// </summary>
    public class CommandBase : IPresenterCommand
    {
        private IEventAggregator _eventAggregator;

            /// <summary>
        /// Occurs when changes occur that affect whether the command should execute.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// Gets or sets the logger.
        /// </summary>
        /// <value>The logger.</value>
        [Dependency]
        public ILoggerFacade Logger { get; set; }

        /// <summary>
        /// Gets or sets the event aggregator.
        /// </summary>
        /// <value>The event aggregator.</value>
        [Dependency]
        public IEventAggregator EventAggregator
        {
            get { return _eventAggregator; }            
            set
            {
                _eventAggregator = value;
                OnEventAggregatorSet();
            }
        }

        /// <summary>
        /// Gets or sets the presenter.
        /// </summary>
        /// <value>The presenter.</value>
        public IPresenter Presenter { get; set; }

        /// <summary>
        /// Defines the method that determines whether the command can execute in its current state.
        /// </summary>
        /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
        /// <returns>
        /// true if this command can be executed; otherwise, false.
        /// </returns>
        public virtual bool CanExecute(object parameter)
        {
            Logger.Log("{0}.CanExecute({1})", GetType().Name, parameter);
            return true;
        }

        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
        public virtual void Execute(object parameter)
        {
            Logger.Log("{0}.Execute({1})", GetType().Name, parameter);
        }

        protected virtual void OnEventAggregatorSet()
        {
        }

        private bool _disposed;
        public void Dispose()
        {
            if (!_disposed)
            {
                OnDispose();
                _disposed = true;
            }
        }

        protected virtual void OnDispose()
        {

        }

    }
}
